<template>
<transition name="popup-slide-up">
    <div class="ebook-popup-list" v-show="fontFamilyVisible">
        <div class="ebook-popup-title">
            <!-- 建议每个字体图标外都加一层wrapper，方便添加事件和样式设置 -->
            <div class="ebook-popup-title-icon" @click="hide">
                <span class="icon-down2"></span>
            </div>
            <span class="ebook-popup-title-text">{{ $t('book.selectFont') }}</span>
        </div>
        <div class="ebook-popup-list-wrapper">
            <div class="ebook-popup-item" v-for="(item,index) in fontFamilyList" :key="index" @click="setFontFamily(item.font)">
                <div class="ebook-popup-item-text" :class="{'selected':isSelected(item)}">{{ item.font }}</div>
                <div class="ebook-popup-item-check" :class="{'selected':isSelected(item)}">
                    <span class="icon-check"></span>
                </div>
            </div>
        </div>
    </div>
</transition>
</template>

<script>
import { ebookMixin } from '../../utils/mixin'
import { FONT_FAMILY } from '../../utils/book'
import { saveFontFamily } from '../../utils/localStorage'
export default {
    mixins: [ebookMixin],
    data() {
        return {
            fontFamilyList: FONT_FAMILY
        };
    },
    methods: {
        hide() { //隐藏字体选择界面
            this.setFontFamilyVisible(false)
        },
        setFontFamily(font) { //设置默认字体为选中的字体，并保存到本地方便刷新
            this.setDefaultFontFamily(font)
            saveFontFamily(this.fileName,font)
            if(font === 'Default') { //默认为新罗马字体
                this.currentBook.rendition.themes.font('Times New Roman')
                console.log('改变为默认字体新罗马')
            }else {//设置字体
                console.log('字体改变为'+font)
                this.currentBook.rendition.themes.font(font)
            }
        },
        isSelected(item) { //判断当前选中字体是否为默认字体
            return this.defaultFontFamily === item.font
        }
    },
};
</script>

<style lang="scss" scoped>
@import '../../assets/styles/global.scss';
.ebook-popup-list {
    position: absolute; 
    bottom: 0;
    left: 0;
    z-index: 300;//覆盖掉菜单和设置栏
    width: 100%;
    background: white;
    box-shadow: 0 px2rem(-4) px2rem(6) rgba(0,0,0,.1);
    display: flex;
    flex-direction: column;
    .ebook-popup-title { 
        position: relative;
        padding: px2rem(15);
        box-sizing: border-box;
        border-bottom: px2rem(1) solid #b8b9bb;
        @include center; //这里让下面两个居中，但因为ebook-popup-title-icon脱离了文档流，所以让ebook-popup-title-text居中了
        .ebook-popup-title-icon { 
            position: absolute;
            left: px2rem(15);
            top: 0;
            height: 100%; //有高度才能用@include center
            font-size: px2rem(16);
            font-weight: bold;
            @include center;
        }
        .ebook-popup-title-text {
            font-size: px2rem(14);
            font-weight: bold;
        }
    }
    .ebook-popup-list-wrapper {
        .ebook-popup-item { 
            display: flex;
            padding: px2rem(15);
            .ebook-popup-item-text { 
                flex: 1;
                font-size: px2rem(14);
                text-align: left;
                &.selected {
                    color: #346cb9;
                    font-weight: bold;
                }
            }
            .ebook-popup-item-check { 
                flex: 1;
                font-size: px2rem(14);
                text-align: right;
                &.selected {
                    color: #346cb9;
                    font-weight: bold;
                }
            }
        }
    }
}
</style>